<?php

namespace Cps\Controller;

use Think\Controller;
use Think\Page;

class PaylogController extends Controller {

    public function __construct() {
        parent::__construct();
        session("[start]");
        if (!isset($_SESSION['cpsinfo']) || $_SESSION['cpsinfo']['id'] < 1) {
            $this->redirect('Login/index');
        }
    }

    public function index() {
        $cpsinfo = $_SESSION['cpsinfo'];
        if($cpsinfo['refer']==0){
            $refer=I("request.refer");
            $model=M('cps_member');
            $refername=$model->field('username')->where("refer='%s'",$refer)->find();
            $this->assign('refname',$refername['username']);
            $this->assign('refer',$refer);
        }  else {
            $refer=$cpsinfo['refer'];
        }
        $game_id = $_POST['game_id'] ? intval($_POST['game_id']) : 0;
        $area_num = $_POST['area_num'] ? intval($_POST['area_num']) : 0;
        $username = $_POST['username'] ? trim(I('post.username')) : '';
        $dt1 = $_POST['data_start'] ? I('post.data_start') : '';
        $dt2 = $_POST['data_end'] ? I('post.data_end') : '';
        //游戏信息
        $gameModel = M('game_list');
        $game = $gameModel->field('id,gamename')->order('id desc')->select();
        $gamelist = json_encode($game);
        $this->assign('game', $gamelist);

        //用户游戏关系表，订单表
        $count = count($this->game_user_relation($refer));

        $Page = new Page($count, 10); // 实例化分页类 传入总记录数
        $show = $Page->show(); // 分页显示输出
        $list = $this->game_user_relation($refer, 1, $Page->firstRow, $Page->listRows, $game_id, $area_num, $username, strtotime($dt1), strtotime($dt2));
        $total=0;
        
        for($i=0;$i<count($list);$i++){
            $total+=$list[$i]['fee_cny'];
        }
        
        $this->assign('game_id', $game_id);
        $this->assign("area_num", $area_num);
        $this->assign("username", $username);
        $this->assign('dt1', $dt1);
        $this->assign('dt2', $dt2);
        $this->assign('page', $show);
        $this->assign('total',$total);
        $this->assign('list', $list);
        $this->display();
    }

    public function serverlist() {
        //区服信息
        if (IS_AJAX) {
            $game_id = intval($_GET['game_id']);
            $serverModel = M('game_server_list');
            $server = $serverModel->field("area_num,areaname")->where("game_id='%d'", $game_id)->select();
            $this->ajaxReturn($server, 'json');
        }
    }

    private function game_user_relation($refer, $flag = 0, $firstRow, $listRows, $game_id, $area_num, $username, $starttime, $endtime) {

        $game_user_relation = M("game_user_relation");
        if ($flag == 0) {
            $sql = "select gur.username,p.order_num,floor(p.fee_cny) as fee_cny,p.pay_time,p.discript from game_user_relation as gur,pay_log as p where gur.refer in(" . $refer . ") and gur.username=p.pay_username and gur.game_id=p.gid and gur.area_num=p.sid and p.flag_game=1";
            return $game_user_relation->query($sql);
        } else {
            $sql = "select gur.username,p.order_num,floor(p.fee_cny) as fee_cny,p.pay_time,p.discript from game_user_relation as gur,pay_log as p ";
            $where = "where gur.refer in(" . $refer . ") and gur.username=p.pay_username and gur.game_id=p.gid and gur.area_num=p.sid and p.flag_game=1";
            if ($game_id != 0) {
                $where.=" and p.gid='" . $game_id . "'";
            }
            if ($area_num != 0) {
                $where.=" and p.sid='" . $area_num . "'";
            }
            if ($username != '') {
                $where.=" and p.pay_username='" . $username . "'";
            }
            if ($starttime != '') {
                $where.=" and p.pay_time>='" . $starttime . "'";
            }
            if ($endtime != '') {
                $where.=" and p.pay_time<='" . $endtime . "'";
            }
            $limit = " limit {$firstRow} , {$listRows}";
            $query = $sql . $where . $limit;
            
            return $game_user_relation->query($query);
        }
    }

}
